package handler

import (
	"context"
	"course/global"
	"course/internal/model/dao"
	course "course/proto"
	"fmt"
	"time"
)

type CourseServer struct {
	course.UnimplementedCourseServer
}

func (c *CourseServer) CourseInfo(ctx context.Context, req *course.CourseInfoRequest) (resp *course.CourseInfoResponse, err error) {
	CourseDao := dao.Course{}
	info := CourseDao.GetAllCourseInfo()
	var res []*course.CourseInfo
	for _, v := range info {
		res = append(res, &course.CourseInfo{
			Id:     int32(v.ID),
			Name:   v.Name,
			Status: int32(v.Status),
			Coach:  v.Coach,
			Price:  float32(v.Price),
			Start:  int32(v.Start.Unix()),
			End:    int32(v.End.Unix()),
		})
	}

	return &course.CourseInfoResponse{Info: res}, nil
}

func (c *CourseServer) CourseDetail(ctx context.Context, req *course.CourseDetailRequest) (resp *course.CourseDetailResponse, err error) {

	CourseDao := dao.Course{}
	info, err := CourseDao.GetDetailCourseInfo(int(req.Id))
	if err != nil {
		return nil, fmt.Errorf("查询失败")
	}

	return &course.CourseDetailResponse{
		Id:     int32(info.ID),
		Name:   info.Name,
		Status: int32(info.Status),
		Coach:  info.Coach,
		Price:  float32(info.Price),
		Start:  int32(info.Start.Unix()),
		End:    int32(info.End.Unix()),
	}, nil
}
func (c *CourseServer) ReservationInfo(ctx context.Context, req *course.ReservationInfoRequest) (resp *course.ReservationInfoResponse, err error) {

	CourseDao := dao.Course{}
	info, err := CourseDao.GetDetailCourseInfo(int(req.CourseId))
	if err != nil {
		return nil, fmt.Errorf("课程信息不存在")
	}

	res := dao.Reservation{
		UserID:    int(req.UserId),
		Course:    info.Name,
		Data:      time.Now(),
		Time:      time.Now(),
		Space:     info.Space,
		Address:   info.Address,
		PersonNum: 2,
		Num:       1,
	}
	err = global.DB.Create(&res).Error
	if err != nil {
		return nil, fmt.Errorf("预约失败")
	}

	return &course.ReservationInfoResponse{
		Msg: "预约成功",
	}, nil
}
func (c *CourseServer) ReservationEnter(ctx context.Context, req *course.ReservationEnterRequest) (resp *course.ReservationEnterResponse, err error) {

	reservation := dao.Reservation{}
	getReservation, err := reservation.GetReservation(int(req.Id))
	if err != nil {
		return nil, fmt.Errorf("你还没有预约")
	}
	Reservation := &dao.Reservation{
		ID:        getReservation.ID,
		UserID:    getReservation.UserID,
		Course:    getReservation.Course,
		Space:     getReservation.Space,
		Address:   getReservation.Address,
		PersonNum: getReservation.PersonNum,
		Num:       getReservation.Num,
		Desc:      req.Desc,
		Status:    getReservation.Status,
	}
	global.DB.Save(&Reservation)

	return &course.ReservationEnterResponse{
		Msg: "预约确认",
	}, nil
}
func (c *CourseServer) GetAllReservation(ctx context.Context, req *course.GetAllReservationRequest) (resp *course.GetAllReservationResponse, err error) {

	reservation := dao.Reservation{}
	info := reservation.GetUserAllReservationInfo(int(req.UserId), int(req.Status))
	var result []*course.ReservationInfo
	for _, v := range info {
		result = append(result, &course.ReservationInfo{
			Id:        int32(v.ID),
			UserId:    int32(v.UserID),
			Course:    v.Course,
			Data:      int32(v.Data.Unix()),
			Times:     int32(v.Time.Unix()),
			Space:     v.Space,
			Address:   v.Address,
			PersonNum: int32(v.PersonNum),
			Num:       int32(v.Num),
			Desc:      v.Desc,
		})
	}
	return &course.GetAllReservationResponse{Info: result}, nil
}
func (c *CourseServer) DelReservation(ctx context.Context, req *course.DelReservationRequest) (resp *course.DelReservationResponse, err error) {

	return &course.DelReservationResponse{
		Msg: "取消预约了",
	}, nil
}
